package com.huaxin.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.request.EquipmentDataSourceCreateReqDTO;
import com.huaxin.device.dto.request.EquipmentDataSourcePageListReqDTO;
import com.huaxin.device.models.EquipmentDataSource;
import com.huaxin.device.service.EquipmentDataSourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "数据源关联")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/device/equipmentDataSource")
public class EquipmentDataSourceController {

    private final EquipmentDataSourceService service;

    @Operation(summary = "创建数据源关联", description = "创建数据源关联")
    @PostMapping("/create")
    public ResponseResult<String> create(@RequestBody @Valid List<EquipmentDataSourceCreateReqDTO> in) {
        return service.create(in);
    }

    @Operation(summary = "获取数据源关联", description = "根据ID获取数据源关联信息")
    @GetMapping("/get")
    public ResponseResult<EquipmentDataSource> get(
            @Parameter(description = "数据源ID", example = "eds_001")
            @RequestParam(value = "id", required = true) String id) {
        EquipmentDataSource entity = service.getByDataSourceId(id);
        return ResponseResult.ok(entity);
    }

    @Operation(summary = "删除数据源关联", description = "删除指定ID的数据源关联")
    @DeleteMapping("/{id}/delete")
    public ResponseResult<String> delete(
            @Parameter(description = "数据源ID", example = "eds_001")
            @PathVariable String id) {
        return service.delete(id);
    }

    @Operation(summary = "分页查询数据源关联", description = "分页查询数据源关联列表")
    @PostMapping("/page")
    public ResponseResult<IPage<EquipmentDataSource>> pageQuery(@RequestBody @Valid EquipmentDataSourcePageListReqDTO in) {
        return service.pageQuery(in);
    }

    @Operation(summary = "切换数据源关联是否为主数据", description = "切换数据源关联是否为主数据")
    @GetMapping("/toggleIsPrimary")
    public ResponseResult<Boolean> toggleIsPrimary(
            @Parameter(description = "数据源ID", example = "eds_001")
            @RequestParam(value = "id", required = true) String id) {
        return service.toggleIsPrimary(id);
    }
}